package com.campus.service.impl;

import com.campus.constant.MessageConstant;
import com.campus.exception.ResourceNotFoundException;
import com.campus.mapper.CuisineTypeMapper;
import com.campus.mapper.ShopDishMapper;
import com.campus.mapper.ShopMapper;
import com.campus.pojo.dto.ShopDTO;
import com.campus.pojo.dto.ShopPageQueryDTO;
import com.campus.pojo.entity.CuisineType;
import com.campus.pojo.entity.Shop;
import com.campus.pojo.entity.ShopDish;
import com.campus.pojo.vo.ShopVO;
import com.campus.result.PageResult;
import com.campus.service.ShopService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopDishMapper shopDishMapper;
    @Autowired
    private CuisineTypeMapper cuisineMapper;

    @Override
    public PageResult<ShopVO> pageQuery(ShopPageQueryDTO shopPageQueryDTO) {
        //1.设置分页参数
        PageHelper.startPage(shopPageQueryDTO.getCurrentPage(), shopPageQueryDTO.getPageSize());

        //2.执行分页查询（只查询店铺基本信息）
        Page<ShopVO> page = shopMapper.pageQuery(shopPageQueryDTO);

        //3.为每个店铺封装菜品信息
        page.forEach(shopVO -> {
            Long shopId = shopVO.getId();
            List<ShopDish> dishList = shopDishMapper.getDishList(shopId);
            shopVO.setDishList(dishList);
        });

        //4.封装pageResult对象并返回
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public ShopVO getShopById(Long id) {
        //1.查询店铺是否存在
        Shop shop = shopMapper.getShopById(id);
        if (shop == null) {
            //不存在
            throw new ResourceNotFoundException(MessageConstant.SHOP_NOT_FOUND);
        }

        //2.查询店铺信息
        ShopVO shopVO = new ShopVO();
        //属性比较多 直接拷贝
        BeanUtils.copyProperties(shop, shopVO);
        //封装菜系名称字段 根据菜系ID查询菜系名称
        CuisineType cuisineType = cuisineMapper.getCuisineTypeName(shopVO.getCuisineType());
        shopVO.setCuisineTypeName(cuisineType.getName());

        //3.查询当前店铺的菜品信息
        List<ShopDish> dishList = shopDishMapper.getDishList(id);
        shopVO.setDishList(dishList);

        //4.封装vo并返回
        return shopVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWithDish(ShopDTO shopDTO) {
        //1.保存店铺基本信息
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDTO, shop);
        //插入后把id封装到shop对象中-options注解
        shopMapper.insert(shop);

        //2.向shop_dish表批量插入菜品信息
        List<ShopDish> dishList = shopDTO.getDishList();
        if (dishList != null && !dishList.isEmpty()) {
            dishList.forEach(dish -> {
                dish.setShopId(shop.getId());
                //这里不能使用aop自动填充 我们设计的aop只能自动填充entity对象
                dish.setCreateTime(LocalDateTime.now());
                dish.setUpdateTime(LocalDateTime.now());
            });
            shopDishMapper.insertBatch(dishList);
        }



    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWithDish(ShopDTO shopDTO) {
        //1.更新店铺基本信息
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDTO, shop);
        shopMapper.update(shop);

        //2.修改店铺对应的菜品信息
        //先删除
        Long shopId = shop.getId();
        shopDishMapper.deleteByShopIds(Arrays.asList(shopId));
        //再批量插入
        List<ShopDish> dishList = shopDTO.getDishList();
        if (dishList != null && !dishList.isEmpty()) {
            dishList.forEach(dish -> {
                dish.setShopId(shopId);
                //这里不能使用aop自动填充 我们设计的aop只能自动填充entity对象
                dish.setUpdateTime(LocalDateTime.now());
                dish.setCreateTime(LocalDateTime.now());
            });
        }
        shopDishMapper.insertBatch(dishList);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Long> ids) {
        //1.删除店铺
        shopMapper.deleteByIds(ids);

        //2.删除店铺对应的菜品
        shopDishMapper.deleteByShopIds(ids);
    }
}

